import Vue from 'vue'
import VueRouter from 'vue-router'
// import HomeView from '../views/HomeView.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/wel'
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/login/Login.vue')
  },
  {
    path: '/home',
    name: 'home',
    meta: {
      auth: true
    },
    component: () => import('../views/home/home.vue'),
    children: [
      {
        path: '/wel',
        name: 'wel',
        component: () => import('../views/wel.vue')
      }, {
        path: '/users',
        name: 'users',
        component: () => import('../views/users/users.vue')
      }, {
        path: '/roles',
        name: 'roles',
        component: () => import('../views/quanxian2/roles.vue')
      }, {
        path: '/rights',
        name: 'rights',
        component: () => import('../views/quanxian2/rights.vue')
      }
    ]
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, form, next) => {
  const flag = to.matched.some(item => {
    return item.meta.auth
  })
  if (!flag) {
    next()
  } else {
    const tok = sessionStorage.getItem('token')
    if (!tok) {
      next({ path: '/login' })
    }
    next()
  }
})

export default router
